Skip to content

Find defects on a surface ​

Use this recipe when the question is "does this product look different from a good one?" Typical cases are scratches, dents, stains or marks on a surface, where defects are rare and no two look alike. You cannot name every defect in advance, so you need an anomaly detection dataset: the model learns what a good product looks like and flags anything different, with one verdict per image, OK or NOK. If the defects come in a few known kinds with many examples of each, Sort products by type may be enough. If the size of the defect matters, use segmentation (Choose the right dataset type).

StepChoice
Dataset«Anomaly detection», with its tags «Ok» and «Nok»
ImagesMostly good products, as varied as the line produces them
LabelingMark good images «Ok» in bulk, and paint the defect on a few bad ones
Results«False rejects» and «Defects found»
FlowReject the product when the model calls it NOK

Collect the images ​

Create a dataset of type «Anomaly detection». The model learns only from the good images, and every defective image is kept for testing it. You need:

  • Many good products, as varied as possible. A dozen good images is too few to learn what a normal product looks like. Use the line's camera and light, and cover different batches, product positions and the reflections the surface really gives. A good product unlike any good image the model has seen will be called NOK.
  • A few defective products of each kind you know. The model does not learn from them, but without them «Defects found» cannot be measured.

A training needs at least 10 labeled images in all.

Label them ​

Anomaly detection datasets come with two tags: «Ok» for normal images and «Nok» for anomalies (Mark images as normal or anomaly).

  • Good images, all at once. In «Labeling», tick the good images and click «Ok» in the bar at the bottom.
  • Paint the defect on a few. Open a defective image, click «Anomaly», and paint over the defect with «Draw» or outline it with «Polygon». This helps the model point to the defect itself, not only flag the image. Smart Labeling draws the mask with one click.
  • Hundreds of images. From a few examples, the auto classifier suggests «Ok» or «Nok» for each image (Classify images automatically by similarity).

Train and read the results ​

In «Models», start a «Production training» (Train your first model). Some anomaly models, such as Padim, make a single pass over the good images: they train in seconds and have no time slider.

When the training ends, choose «All» in the selector above the scores. The «Test» images may contain no defect, and the scores then stay empty (Read the results of an anomaly detection model). There are three scores:

  • «Accuracy»: the share of images, good and defective, that get the right label.
  • «False rejects»: the share of good products called NOK, which the line would throw away. Lower is better.
  • «Defects found»: the share of defective products caught. The rest would reach the customer.

Judge the model on the error your line can least afford. In «Evaluation images», each image shows its anomaly score: from 50 % up, the model calls it NOK. Set «Prediction» to «Incorrect» to see only the mistakes. To know which images it got wrong, go by the matrix and the frames in «Evaluation images» rather than by the viewer that opens when you click an image.

Reject it in a flow ​

The inferencer node runs the model on each image (Run a trained model on an image). Its answer in msg.inference describes the unusual area it found: its outline (polygons), a confidence, and the share of the image it covers (area).

Before writing the rule, add a debug node set to msg.inference and compare its answers for a good and a defective image, sent from the dataset with from-dataset (Send dataset images through a flow). A function node then turns the answer into OK or NOK, and a switch node sends NOK products to the reject (Decide pass or fail from the predictions). A rule on area can let very small marks through.

Show it to the operators ​

rp-add-masks paints the areas the model found over the image (Draw results on the image). On a panel, the image canvas shows the image with the outlines on top and a result card. isAnomalous makes the card say NOK (true) or OK (false) (Show inspection results on a panel). The «POLYGONS» checkbox hides the outlines so the operator can see the bare surface (Open and operate a panel).

Improve it ​

  • Too many false rejects. Add good images like the wrongly rejected ones, with other positions, reflections or batches, mark them «Ok» and train again.
  • Defects that get through. Check that they are marked «Nok», paint the defect on a few, and try another model from the list, such as Dinomaly.
  • Images from the line. Save the rejected products and upload them with dataset-upload. They arrive without a tag. The good products among them are false rejects, and they are the most useful images to mark «Ok» (Collect images from a flow into a dataset).
  • Compare. Train again with «Copy from another training», so that only the images change, then choose the new model in the inferencer node (Retrain a model after adding images).

Next steps ​